package com.villaday.idf.adapter;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;

import com.villaday.idf.R;
import com.villaday.idf.base.AbsRecyclerViewAdapter;
import com.villaday.idf.base.OnAdapterCallbackListener;
import com.villaday.idf.model.SortModel;
import com.villaday.idf.model.allVipMembersEntity;
import com.villaday.idf.model.vipMembersEntity;

import java.util.List;

/**
 * 首页动态列表
 */

public class AddMemberAdapter extends AbsRecyclerViewAdapter<allVipMembersEntity.BodyBean> {
    private OnAdapterCallbackListener onAdapterCallbackListener;
    private View.OnClickListener onClickListener;
    private Context mContext;
    private List<allVipMembersEntity.BodyBean> mData;
    private String modleId;

    public AddMemberAdapter(Context context, OnAdapterCallbackListener onAdapterCallbackListener, String modleId) {
        super(context, R.layout.recyclerview_add_member, R.layout.item_page_loading,
                R.layout.item_page_bottom);
        this.onAdapterCallbackListener = onAdapterCallbackListener;
        this.mContext = context;
        this.modleId = modleId;
    }

    @Override
    public int getItemType(allVipMembersEntity.BodyBean d) {
        return d.getType();
    }

    @Override
    public void onBindHolder(AbsRecyclerViewAdapter.RecyclerViewHolder holder, allVipMembersEntity.BodyBean d, int position) {
        switch (d.getType()) {
            case 0: {
                int section = getSectionForPosition(position);
                //如果当前位置等于该分类首字母的Char的位置 ，则认为是第一次出现
                if (position == getPositionForSection(section)) {
                    holder.setViewVisible(R.id.tag, View.VISIBLE)
                            .bindTextView(R.id.tag, d.getLetters());
                } else {
                    holder.setViewVisible(R.id.tag, View.GONE);
                }


                holder.bindTextView(R.id.name, d.getNickName())
                        .bindSimpleDraweeView(R.id.sdv_head, d.getHeaderUrl())
                        .setOnClickListener(R.id.iv_choose)
                        .setViewSelected(R.id.iv_choose, d.isSelected())
                        .setOnClickListener(R.id.ll_member_layout);

                ImageView ivChoice = (ImageView) holder.getView(R.id.iv_choose);
                if (modleId.equals(d.getGroup().getId())) {
                    ivChoice.setImageDrawable(mContext.getResources().getDrawable(R.drawable.ico_check_disabled));
                } else {
                    if (d.isSelected()) {
                        ivChoice.setImageDrawable(mContext.getResources().getDrawable(R.drawable.svip_chose));
                    } else {
                        ivChoice.setImageDrawable(mContext.getResources().getDrawable(R.drawable.svip_unchose));
                    }
                }
            }
            break;
            case 1://滑到底，加载下一页
                onAdapterCallbackListener.onCallback();
                break;
        }
    }


    /**
     * 根据ListView的当前位置获取分类的首字母的char ascii值
     */
    public int getSectionForPosition(int position) {
        return mData.get(position).getLetters().charAt(0);

    }

    /**
     * 根据分类的首字母的Char ascii值获取其第一次出现该首字母的位置
     */
    public int getPositionForSection(int section) {
        if (mData != null && mData.size() > 0) { //防止没数据时点击报错
            for (int i = 0; i < getItemCount(); i++) {
                String sortStr = mData.get(i).getLetters();
                char firstChar = sortStr.toUpperCase().charAt(0);
                if (firstChar == section) {
                    return i;
                }
            }
        }
        return -1;
    }

    public void setValue(List<allVipMembersEntity.BodyBean> list) {
        this.mData = list;
    }
}
